package com.easywan.web.controller.admin;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.easywan.core.JSPModelAndView;
import com.easywan.core.RedirectModelAndView;
import com.easywan.service.AdminMenueService;
import com.easywan.util.Utils;
import com.easywan.vo.AdminMenue;
import com.easywan.vo.Pages;

@Controller
@RequestMapping("admin/AdminMenue")
public class AdminMenueControllerAdmin {
	private static Logger logger = Logger.getLogger(AdminMenueControllerAdmin.class);
	@Autowired 
	AdminMenueService service;
	@RequestMapping("index")
	public ModelAndView index(HttpServletRequest request,HttpServletResponse response,Pages pages,
			@RequestParam("pname")String pname,
			@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/AdminMenue/index");
		AdminMenue tempAdminMenue = new AdminMenue();
		tempAdminMenue.setPage(pages);
		List<AdminMenue> lists = service.list(tempAdminMenue);
		view.addObject("data",lists);
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("page", pages);
		return view;
	}
	@RequestMapping("add")
	public ModelAndView add(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pname")String pname,@RequestParam("name")String name){
		JSPModelAndView view = new JSPModelAndView("/admin/AdminMenue/modify");
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("action", "add");
		AdminMenue tempAdminMenue = new AdminMenue();
		tempAdminMenue.setPid(0);
		List<AdminMenue> lists = service.listBySelectTive(tempAdminMenue);
		view.addObject("adminMenues",lists);
		return view;
	}
	@RequestMapping("add_chk")
	public ModelAndView add_chk(HttpServletRequest request,HttpServletResponse response,AdminMenue AdminMenue){
		ModelAndView view = new RedirectModelAndView("admin/AdminMenue/index");
		service.add(AdminMenue);
		view.addObject("pname","");
		view.addObject("name","");
		return view;
	}
	
	@RequestMapping("modify")
	public ModelAndView modify(HttpServletRequest request,HttpServletResponse response,
			@RequestParam("pname")String pname,@RequestParam("name")String name,
			@RequestParam("id")Integer id){
		JSPModelAndView view = new JSPModelAndView("/admin/AdminMenue/modify");
		view.addObject("pname", pname);
		view.addObject("name", name);
		view.addObject("action", "modify");
		AdminMenue tempAdminMenue = new AdminMenue();
		tempAdminMenue.setPid(0);
		List<AdminMenue> lists = service.listBySelectTive(tempAdminMenue);
		view.addObject("adminMenues",lists);
		view.addObject("data",service.findById(id));
		return view;
	}
	@RequestMapping("modify_chk")
	public ModelAndView modify_chk(HttpServletRequest request,HttpServletResponse response,
			AdminMenue AdminMenue){
		ModelAndView view = new RedirectModelAndView("admin/AdminMenue/index");
		System.out.println(AdminMenue);
		service.modify(AdminMenue);
		view.addObject("pname","");
		view.addObject("name","");
		return view;
	}
	@RequestMapping("del")
	public void del(HttpServletRequest request,HttpServletResponse response,@RequestParam("id")Integer id){
		service.deleteById(id);
		Utils.sendSuccessMsg(response);
	}
}
